package com.module.antiFake.manager;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.antiFake.dao.AntiFakeWarnDao;
import com.module.antiFake.vo.AntiFakeWarn;

public class AntiFakeWarnManager extends GenericHibernateManager<AntiFakeWarn, Long>{
	private AntiFakeWarnDao dao = AntiFakeWarnDao.getInstance();
	private static AntiFakeWarnManager service;
	public synchronized static AntiFakeWarnManager getInstance() {
		if(service == null){
			service = new AntiFakeWarnManager();
		}
		return service;
	}

	/**
	 * 通过页面查询条件列出预警记录;
	 * @param bean
	 * @return 
	 */
	public Page findWarnFromWebSearch( AntiFakeWarn antiFakeWarn, Page page) throws Exception{
		Session session = this.getSession();
		try {
			dao.getList(session, antiFakeWarn, page);
		} catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}
		return page;	
	}

//	public void getOrganizationWarnMessageCount(AntiFakeWarn antiFakeWarn) throws Exception{
//		Session session = this.getSession();
//		try {
//			antiFakeWarn.setUntreatedNum(dao.countUntreateNumByOrgId(session,antiFakeWarn.getOrgid()));
//		} catch (Exception e) {
//			throw e;
//		}finally{
//			this.closeSession();
//		}
//	}
//
	/**
	 * 根据id查询预警信息
	 * @param id
	 * @return 
	 */
	public AntiFakeWarn findById(Long id) throws Exception{
		try{
			AntiFakeWarn antiFakeWarn = dao.getWarnById(this.getSession(),id);
			return antiFakeWarn;
		}catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}				
	}
	/**
	 * 处理预警信息
	 * @param webWarn
	 * @return
	 * @throws Exception
	 */
	public boolean updateAntiFakeWarnProcessState(AntiFakeWarn webWarn) throws Exception{
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try{
			AntiFakeWarn antiFakeWarn = dao.get(session,webWarn.getId());
			antiFakeWarn.setProcessState(webWarn.getProcessState());
			antiFakeWarn.setProcessId(webWarn.getProcessId());
			antiFakeWarn.setProcessName(webWarn.getProcessName());
			antiFakeWarn.setProcessTime(new Date());
			dao.update(session,antiFakeWarn);
			tx.commit();
			return true;
		}catch (Exception e) {
			tx.rollback();
			throw e;
		}finally{
			this.closeSession();
		}		
	}
	/**
	 * 预警信息定时扫描任务
	 * @throws Exception
	 */
	public void scanTimesWarn() throws Exception{
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		try {
			dao.scanTimesWarn(session);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally{
			this.closeSession();
		}
	}
	public AntiFakeWarn getNextAntiFakeWarn(AntiFakeWarn bean) throws Exception{
		try{
			AntiFakeWarn antiFakeWarn = dao.getNextAntiFakeWarn(this.getSession(),bean.getId(),bean.getOrgid());
			return bean;
		}catch (Exception e) {
			throw e;
		}finally{
			this.closeSession();
		}	
	}
	@Override
	public GenericHibernateDao<AntiFakeWarn, Long> getDao() {
		return dao;
	}
}
